package hotswap.outboundRequest;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsl.NCCUtil;
import com.jsl.NCCloud21Service;
import com.jsl.action.OutboundRequestInterface;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.soa.workflow.request.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OutboundRequestInterfaceImpl implements OutboundRequestInterface {

    private static final Log log = LogFactory.getLog(OutboundRequestInterfaceImpl.class);

    @Override
    public String execute(RequestInfo info) {
        log.info("**********************进入 AddOutboundRequestAction **********************");

        String requestid = info.getRequestid();
        String workflowid = info.getWorkflowid();
        String formtable = NCCUtil.getFormtable(workflowid);

        log.info("[AddOutboundRequestAction.execute] workflowid:" + workflowid + " formtable:" + formtable + " requestid:" + requestid);
        MainTableInfo mainTableInfo = info.getMainTableInfo();
        DetailTableInfo detailTableInfo = info.getDetailTableInfo();

        try {
            // 输出主表参数
            ObjectMapper objectMapper1 = new ObjectMapper();
            String mainTableInfoString = objectMapper1.writeValueAsString(mainTableInfo);
            log.info("[AddOutboundRequestAction.execute] mainTableInfo:" + mainTableInfoString);
            // 输出明细表参数
            ObjectMapper objectMapper2 = new ObjectMapper();
            String detailTableInfoString = objectMapper2.writeValueAsString(detailTableInfo);
            log.info("[AddOutboundRequestAction.execute] detailTableInfo:" + detailTableInfoString);
            List<Map<String, Object>> mapList = new ArrayList<>();

            // 构造请求参数
            Map<String, Object> requestMap = getRequestMap(mainTableInfo, detailTableInfo);
            mapList.add(requestMap);
            String requestBody = JSON.toJSONString(mapList);
            log.info("[AddOutboundRequestAction.execute] requestBody:" + requestBody);
            String apiUrl = "/nccloud/api/ic/sapplybill/save";

            // 拿到返回数据，传入apiUrl、请求参数
            String result = NCCloud21Service.pushData(apiUrl, requestBody);

            log.info("[AddOutboundRequestAction.execute] result:" + result);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return null;
    }


    private static Map<String, Object> getRequestMap(MainTableInfo mainTableInfo, DetailTableInfo detailTableInfo) {
        Map<String, Object> resultMap = new HashMap<>();


        // 构造主体请求参数
        Map<String, Object> icSapplyHMap = new HashMap<>();
        // 项目编码
        String projectCode = "";
        // 仓库主键
        String warehouseId = "";

        for (Property property : mainTableInfo.getProperty()) {
            String name = property.getName();
            // 项目编码
            if (name.equals("nc21xmbh")) {
                projectCode = property.getValue();
                icSapplyHMap.put("vdef9", property.getValue());
            }


            // 单据编号
            if (name.equals("djbh")) {
                projectCode = property.getValue();
                icSapplyHMap.put("vdef21", property.getValue());
            }

            // 出库申请编码
            if (name.equals("nc21ckjylxllk")) {
                icSapplyHMap.put("vtrantypecode", property.getValue().replace("4D", "4455"));
            }
            // 公司代号
            if (name.equals("fgs")) {
                icSapplyHMap.put("pk_org", property.getValue());
            }
            // 领料部门
            if (name.equals("bmdh")) {
                icSapplyHMap.put("cdptvid", property.getValue());
            }
            // 领料部门
            if (name.equals("bmdh")) {
                icSapplyHMap.put("cdptid", property.getValue());
            }
            // 领料人编码
            if (name.equals("rybm")) {
                icSapplyHMap.put("cbizid", property.getValue());
            }
            // 制单时间
            if (name.equals("djsj")) {
                icSapplyHMap.put("dbilldate", property.getValue() + ":00");
            }
            // 仓库编码
            if (name.equals("ck")) {
                warehouseId = property.getValue();
            }
        }

        // 制单人
        icSapplyHMap.put("billmaker", "hboa");
        // 审批人
        icSapplyHMap.put("approver", "hboa");
        // 备注
        icSapplyHMap.put("vnote", "");
        // 合同号
        icSapplyHMap.put("vdef1", "");
        // 用途
        icSapplyHMap.put("vdef10", "");

        resultMap.put("ic_sapply_h", icSapplyHMap);

        // 构造明细请求参数
        List<Map<String, Object>> mapList = new ArrayList<>();
        DetailTable[] detailTableList = detailTableInfo.getDetailTable();
        DetailTable detailTable1 = detailTableList[0];
        for (int i = 0; i < detailTable1.getRow().length; i++) {
            Row row = detailTable1.getRow(i);
            Map<String, Object> map = new HashMap<>();
            // 行号
            map.put("crowno", i * 10);
            for (Cell cell : row.getCell()) {
                if (cell.getName().equals("bz")) {
                    // 行备注
                    map.put("vnotebody", cell.getValue());
                }
                if (cell.getName().equals("wlbm")) {
                    // 物料编码
                    map.put("cmaterialvid", cell.getValue());
                }
                if (cell.getName().equals("wldwzj")) {
                    // 主单位
                    map.put("cunitid", cell.getValue());
                }
                if (cell.getName().equals("wldwzj")) {
                    // 单位
                    map.put("castunitid", cell.getValue());
                }
                if (cell.getName().equals("sl")) {
                    // 数量
                    map.put("nassistnum", cell.getValue());
                }
            }
            // 项目编码
            map.put("cprojectid", projectCode);
            // 计划领用日期
            map.put("dplanrecvdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            // 仓库编码
            map.put("cwarehouseid", warehouseId);
            mapList.add(map);
        }

        resultMap.put("ic_sapply_b", mapList);

        return resultMap;

    }


}
